package com.hs.controller;

import com.hs.config.R;
import com.hs.entity.News;
import com.hs.entity.User;
import com.hs.entity.YxModel;
import com.hs.service.NewsService;
import com.hs.service.UserService;
import com.hs.service.YxModelService;
import com.hs.tool.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@RequestMapping("admin")
@Controller
public class AdminController {
    @Autowired
    UserService userService;
    @Autowired
    YxModelService yxModelService;
    @Autowired
    NewsService newsService;

    @RequestMapping("/userList")
    public String userList(HttpSession session, Model model, User user){
        if(user.getPageSize() == null){
            user.setPageSize(10);
        }
        if(user.getCurrentPage() == null ){
            user.setCurrentPage(0);
        }
        if(user.getFlag() == null){
            user.setFlag(0);
        }
        user.setCurrentPage(user.getCurrentPage() + user.getFlag());
        user.setPageStart(user.getCurrentPage() *  user.getPageSize());
        user.setRole(2);
        model.addAttribute("page",userService.page(user));
        return "admin/user_list";
    }

    @RequestMapping("insert")
    @ResponseBody
    public R insert(YxModel record, Model model) {
        if (record == null) {
            return R.FAILURE("请填写模型关键信息！", null);
        }
        if (StringTools.isEmpty(record.getName())) {
            model.addAttribute("msg", "请填写用户名");
            return R.FAILURE("请填写用户名", null);
        }
        if (StringTools.isEmpty(record.getImgUrl())) {
            model.addAttribute("msg", "请选择图片");
            return R.FAILURE("请选择图片", null);
        }
        int count = yxModelService.selectByConditionCount(record);
        if (count > 0) {
            return R.FAILURE("模型已存在", null);
        }
        int i = yxModelService.insertSelective(record);
        if (i > 0) {
            return R.SUCCESS("", i);
        }
        return R.FAILURE("", 0);
    }

    @RequestMapping("update")
    @ResponseBody
    public R update(YxModel record, Model model) {
        if (record == null) {
            return R.FAILURE("请填写模型关键信息！", null);
        }
        if (record.getId() == null) {
            return R.FAILURE("请填写key值！", null);
        }
        if (StringTools.isEmpty(record.getName())) {
            model.addAttribute("msg", "请填写用户名");
            return R.FAILURE("请填写用户名", null);
        }
        if (StringTools.isEmpty(record.getImgUrl())) {
            model.addAttribute("msg", "请选择图片");
            return R.FAILURE("请选择图片", null);
        }
        int i = yxModelService.updateByPrimaryKeySelective(record);
        if (i > 0) {
            return R.SUCCESS("", i);
        }
        return R.FAILURE("", 0);
    }

    @RequestMapping("edit")
    public String edit(YxModel record, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if(user.getRole() != 1){
            return "error";
        }
        YxModel yxModel = yxModelService.selectByPrimaryKey(record.getId());
        session.setAttribute("user",session.getAttribute("user"));
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("yxModel",yxModel);
        return "admin/model_edit";
    }
    @RequestMapping("newsAdd")
    public String newsAdd(YxModel record, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if(user.getRole() != 1){
            return "error";
        }
        YxModel yxModel = yxModelService.selectByPrimaryKey(record.getId());
        session.setAttribute("user",session.getAttribute("user"));
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("yxModel",yxModel);
        return "admin/news_add";
    }

    @RequestMapping("newsInsert")
    @ResponseBody
    public R newsInsert(News record, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if(user.getRole() != 1){
            return R.FAILURE("无权限","");
        }
        if(StringTools.isEmpty(record.getTitle())){
            return R.FAILURE("请填写抬头","");
        }
        if(StringTools.isEmpty(record.getSubject())){
            return R.FAILURE("请填写简介","");
        }
        if(StringTools.isEmpty(record.getImgUrl())){
            return R.FAILURE("请填写图片","");
        }
        if(StringTools.isEmpty(record.getUrl())){
            return R.FAILURE("请填写外部访问地址","");
        }

        session.setAttribute("user",session.getAttribute("user"));
        model.addAttribute("user",session.getAttribute("user"));
        int i =newsService.insertSelective(record);
        if(i > 0){
            return R.SUCCESS("成功",i);

        }
        return R.FAILURE("新增失败","");
    }



}
